const app = getApp(),  siteroot = getApp().siteInfo.siteroot;
let rewardedVideoAd = null;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: [],
    count: 0,
    goods_qrcode: '/static/sqimg.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (t) {
    var a = this, id = t.id || 0;
    wx.request({
      url: siteroot + 'wxapp/api/getDuobaoInfo',
      data: {id:id, openid: wx.getStorageSync('openid')},
      success: function(res) {
        var e = res.data;
        console.log(e);
        a.setData({
          info: e.data,
          count: e.data.count
        })
      }
    });
    if(wx.createRewardedVideoAd) {
      rewardedVideoAd = wx.createRewardedVideoAd({ adUnitId: 'adunit-c5e84b6ddfd930f3' });
      rewardedVideoAd.onLoad(() => {
        console.log('onLoad event emit')
      })
      rewardedVideoAd.onError((err) => {
        console.log('onError event emit', err)
      })
      rewardedVideoAd.onClose((res) => {
        console.log('onClose event emit', res);
        // 用户点击了【关闭广告】按钮
        if (res && res.isEnded) {
          // 正常播放结束，可以下发游戏奖励
          wx.redirectTo({
            url: '/pages/dbcg/dbcg?id=' + a.data.info.id,
          })
        } else {
          // 播放中途退出，不下发游戏奖励
          wx.showToast({
            title: '视频终止，无法获取抽奖码',
            icon: 'none',
            duration: 2000
          })
        }
      })
    }
  },
  getShare: function() {
    wx.redirectTo({
      url: '/pages/share/share',
    })
  },
  duobaoLook: function(e) {
    var id = e.currentTarget.id;
    wx.redirectTo({
      url: '/pages/duobao/look?id=' + id,
    })
  },
  rewardedAd: function() {
    rewardedVideoAd.show()
    .catch(() => {
      rewardedVideoAd.load()
      .then(() => rewardedVideoAd.show())
      .catch(err => {
        console.log('激励视频 广告显示失败')
      })
    });
  },
  lookcode: function() {
    wx.redirectTo({
      url: '/pages/lookcode/lookcode?id=' + this.data.info.id,
    })
  },
  lookrule: function() {
    wx.request({
      url: siteroot + 'wxapp/api/getCjRule',
      success: function(e) {
          console.log(e);
          wx.showModal({
            title: '抽奖规则',
            content: e.data.data,
            showCancel: false,
            confirmText: '知道了'
          })
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})